/********************************************************************************
 * Project Name		[JavaEE_JDBC]
 * File Name     	[JdbcUtils.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package pool.dbcp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class JdbcUtils {

	private static JdbcUtils instance;

	private static DataSource dataSource;

	static {
		Properties properties = new Properties();
		try {
			String dbcpConfigurationFilePath = "pool/dbcp/dbcpconfig.properties";
			properties.load(JdbcUtils.class.getClassLoader().getResourceAsStream(dbcpConfigurationFilePath));
			dataSource = BasicDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	private JdbcUtils() {
		super();
	}

	/**
	 * 获得一个JDBC工具类的实例
	 * 
	 * @return
	 */
	public static JdbcUtils getInstance() {
		if (instance == null) {
			synchronized (JdbcUtils.class) {
				if (instance == null) {
					instance = new JdbcUtils();
				}
			}
		}
		return instance;
	}

	/**
	 * 获得数据库连接
	 * 
	 * @return
	 * @throws Exception
	 */
	public Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}

	/**
	 * 释放资源
	 * 
	 * @param resultSet
	 * @param statement
	 * @param connection
	 */
	public void close(ResultSet resultSet, Statement statement, Connection connection) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} finally {
						if (connection != null) {
							try {
								// 正常释放连接,
								// 这里的Connection会被DynamicProxyConnection拦截,
								// 然后使用连接池的释放方式[放回连接池]
								connection.close();
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		}

		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (connection != null) {
					try {
						connection.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 开始事务
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	public void beginTransaction(Connection connection) throws SQLException {
		if (connection != null) {
			connection.setAutoCommit(false);
		}
	}

	/**
	 * 提交事务
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	public void commit(Connection connection) throws SQLException {
		if (connection != null) {
			connection.commit();
		}
	}

	/**
	 * 回滚事务
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	public void rollback(Connection connection) throws SQLException {
		if (connection != null) {
			connection.rollback();
		}
	}
}
